<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>基本资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
    <style>
        .layui-form-item .layui-input-company {
            width: auto;
            padding-right: 10px;
            line-height: 38px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layuimini-form">
            <input type="hidden" name="id"/>
            <div class="layui-form-item">
                <label class="layui-form-label required">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="username" lay-verify="required|username" lay-reqtext="用户名不能为空"
                           placeholder="请输入用户名"
                           value=""
                           class="layui-input" disabled>
                    <tip>填写自己管理账号的名称</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">昵称</label>
                <div class="layui-input-block">
                    <input type="text" name="nickname" lay-verify="required|nickname" lay-reqtext="昵称不能为空"
                           placeholder="请输入昵称"
                           value=""
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="true" title="男" checked="">
                    <input type="radio" name="sex" value="false" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">手机</label>
                <div class="layui-input-block">
                    <input type="number" name="phone" lay-verify="required|phone" lay-reqtext="手机不能为空"
                           placeholder="请输入手机"
                           value=""
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">邮箱</label>
                <div class="layui-input-block">
                    <input type="email" name="email" lay-verify="required|email" placeholder="请输入邮箱" value=""
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn">确认修改</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/static/admin/layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/admin/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.js"></script>
<script>
    layui.use(['form', 'miniTab'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniTab = layui.miniTab,
            $ = layui.$;

        //获取用户信息
        $.ajax({
            url: '/admin/user/' + localStorage.getItem("userId") + '/info',
            type: 'GET',
            success: function (result) {
                let user = result.data;
                $('.layui-form input[name="id"]').val(user.id);
                $('.layui-form input[name="username"]').val(user.username);
                $('.layui-form input[name="nickname"]').val(user.nickname);
                $('.layui-form input[name="sex"]').val([user.sex]);
                $('.layui-form input[name="phone"]').val(user.phone);
                $('.layui-form input[name="email"]').val(user.email);
                form.render();
            }
        });

        //自定义验证规则
        form.verify({
            //验证昵称
            nickname: function (value) { //value：表单的值、item：表单的DOM对象
                if (value.length < 3 || value.length > 16) {
                    return '昵称长度为3-16个字符'
                }
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '昵称不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '昵称首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '昵称不能全为数字';
                }
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            axios({
                method: 'put',
                url: '/admin/user/info',
                data: data.field,
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                },
            }).then((response) => {
                Swal.fire({
                    icon: 'success',
                    title: '修改成功',
                    showConfirmButton: false,
                    timer: 1500,
                    onClose: () => {
                        miniTab.deleteCurrentByIframe();
                    }
                });
            }).catch((error) => {
                Swal.fire({
                    icon: 'error',
                    title: '修改失败！',
                    text: ` ${error.response.data.message}`,
                });
            })
            return false;
        });

    });
</script>
</body>
</html>